<?php
/*
	报告单查看
	先输入手机号、验证码登陆。登陆成功后进入报告单查看页面。查看页面从上往下排布报告单图片。
	
*/
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'login';
set_html('title','妇幼保健计划生育服务中心');

if($_RQ['op']=='login'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!check_sms_code($post['smscode'],$post['mobile'])){
			exi('验证码错误！','error');
		}
		$reports=pdo_get('core_report',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$post['mobile']));
		if(!$reports){
			exi('用户不存在！','error');
		}
		if($_SESSION['openid'] && $reports['openid']!=$_SESSION['openid']){
			pdo_update('core_report',array('openid'=>$_SESSION['openid']),array('id'=>$reports['id']));
		}
		$_SESSION['report_mobile']=$post['mobile'];
		exi('登陆成功！','','look');
	}
	
	if($_SESSION['openid']){
		//根据openid检查用户是否存在
		$reports=pdo_get('core_report',array('uniacid'=>$_SESSION['uniacid'],'openid'=>$_SESSION['openid']));
		if($reports){
			$_SESSION['report_mobile']=$reports['mobile'];
			exi('','','look');
		}
	}
	$return=array();
	exi($return);
}
if($_RQ['op']=='look'){
	if(!$_SESSION['report_mobile']){
		exi('请先登陆！','','login');
	}
	$report=pdo_get('core_report',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$_SESSION['report_mobile']));
	$report['reports']=$report['reports']?json_decode($report['reports'],true):array();
	$return['report']=$report;
	exi($return);
}
if($_RQ['op']=='send'){
	$mobile=$_POST['mobile'];
	if(!$mobile){
		$post=get_postdata();
		$mobile=$post['mobile'];
	}
	if(strlen($mobile)!=11 || $mobile[0]!='1' || !is_numeric($mobile)){
		exi('手机号格式错误！','error');
	}
	$reports=pdo_get('core_report',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$mobile));
	if(!$reports){
		exi('用户不存在！','error');
	}
	$code=rand(1000,9999);
	$verify=array(
		'mobile'=>$mobile,
		'code'=>$code,
	);
	if($_SESSION['uniacid']==205){
		$credit2=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
		if($credit2<0.05){
			exi('短信发送失败，请联系系统管理员！','error');
		}
		$_SESSION['uniacid']=1;
		$setting=uni_setting('register');
		$template_id=$setting['dxmb'];
		buffer_save($verify,'core_sms_verify');
		$content=array('code'=>$code);
		$result=cfc('jy_alisms')->sendsms($mobile,$content,$template_id);
		$_SESSION['uniacid']=205;
		if(is_error($result)){
			exi('验证码发送失败：'.$result['message'],'error');
		}
		//此处进行短信计费
		$params=array(
			'type'=>2,
			'fee'=>0.05,
			'remark'=>'短信服务费',
		);
		model('core/web')->account_credit($params);
		exi('success');
	}
	$setting=uni_setting('register');
	$template_id=$setting['dxmb'];
	if(!$template_id){
		exi('管理员未设置系统短信验证码模版，请联系管理员设置！','error');
	}
	buffer_save($verify,'core_sms_verify');
	$content=array('code'=>$code);
	//
	$result=cfc('jy_alisms')->sendsms($mobile,$content,$template_id);
	if(is_error($result)){
		exi('验证码发送失败：'.$result['message'],'error');
	}
	if(!$result){
		exi('error');
	}else{
		exi('success');
	}

}